package com.deer.wms.busine.tactic.web;

import com.deer.wms.busine.tactic.model.pickTactic.PickTactic;
import com.deer.wms.busine.tactic.model.pickTactic.PickTacticCriteria;
import com.deer.wms.busine.tactic.service.PickTacticService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by ll on 2020/02/02.
*/
@RestController
@RequestMapping("/pick/tactics")
public class PickTacticController {

    @Autowired
    private PickTacticService pickTacticService;

    @PostMapping("/add")
    public Result add(@RequestBody PickTactic pickTactic) {
        List<PickTactic> list = pickTacticService.findList(new PickTacticCriteria(pickTactic.getAllocationRuleCode()));
        if(list.size()>0){
            return ResultGenerator.genFailResult(CommonCode.CODE_EXISTS);
        }
        pickTacticService.save(pickTactic);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        pickTacticService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        pickTacticService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody PickTactic pickTactic) {
        List<PickTactic> lists = pickTacticService.verify(new PickTacticCriteria(pickTactic.getAllocationRuleCode(),pickTactic.getPickTacticId()));
        if(lists.size()>0){
            return ResultGenerator.genFailResult(CommonCode.CODE_EXISTS);
        }
        pickTacticService.updateById(pickTactic);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        PickTactic pickTactic = pickTacticService.getById(id);
        return ResultGenerator.genSuccessResult(pickTactic);
    }

    @GetMapping("/list")
    public Result list(PickTacticCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTactic> list = pickTacticService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
